package com.syx.security.Handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.syx.commoncore.Model.Result;
import com.syx.commoncore.enums.Resultcode;
import com.syx.security.Exception.ServiceException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<?> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不⽀持'{}'请求", requestURI, e.getMethod());
        return Result.failure(Resultcode.ERROR);


    }

    @ExceptionHandler(RuntimeException.class)
    public Result<?> handleRuntimeException(RuntimeException e, HttpServletRequest
            request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发⽣异常.", requestURI, e);
        return Result.failure(Resultcode.ERROR);
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发⽣异常.", requestURI, e);
        return Result.failure(Resultcode.ERROR);

    }
    @ExceptionHandler(ServiceException.class)
    public Result ServiceException(ServiceException e,HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.error("[业务出现异常]:"+e.getMessage()+"[错误请求的位置为]:"+requestURI);
        //如果不为空
        if(e.getErrorResult()!=null){
            return Result.failure(e.getErrorResult().getCode(),e.getErrorResult().getMessage());

        }

        if (e.getMessage()==null||e.getMessage().equals("")){
            //没有报错信息的话就会出现问题
            return Result.failure(Resultcode.ERROR_SERVICES);

        }

        //全局异常处理
        return Result.failure(e.getResultcode());
    }
    @ExceptionHandler(BindException.class)
    public Result<Void> handleBindException(BindException e) {
        log.error(e.getMessage());
        String message = join(e.getAllErrors(),DefaultMessageSourceResolvable::getDefaultMessage, ", ");
        return Result.failure(Resultcode.FAILED_PARAMS_VALIDATE.getCode(), message);
    }

    private <E> String join(Collection<E> collection, Function<E, String> function, CharSequence delimiter) {
        if (CollUtil.isEmpty(collection)) {
            return StrUtil.EMPTY;
        }
        return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter));
    }




}
